//index.js
//获取应用实例
var app = getApp();
var CommonUtils = app.CommonUtils;
var Keys = require("../../assets/data/keys.js");

Page({
  data: {
    addressList: [],
    isSelectAddress: false,
    login_id: '',
  },

  onLoad: function (e) {
    this.data.login_id = app.login_id;
    this.data.isSelectAddress = e.isSelectAddress;
    if (this.data.addressMode == !!1) {
      wx.setNavigationBarTitle({
        title: '选择地址'
      })
    }
  },


  onShow: function () {
    this.initAddress();
  },

  /**
   * 初始化地址列表
   */
  initAddress: function () {
    var _this = this;
    wx.showLoading()
    wx.request({
      url: Keys.data.url_Address_List,
      data: {
        DataType: "Address_List",
        uid: app.login_id
      },
      success: function (res) {
        if (res.data.isok == "true") {
          _this.setData({
            addressList: res.data.datas
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },


  /**
   * 编辑地址
   */
  btnEditAddress: function (e) {
    var _this = this;
    wx.navigateTo({
      url: "/pages/address-add/index?haid=" + _this.data.addressList[e.currentTarget.dataset.index].HA_ID
    })
  },

  /**
   * 删除地址
   */
  btnDelAddress: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index;
    var a_id = _this.data.addressList[e.currentTarget.dataset.index].HA_ID;
    wx.showModal({
      title: '提示',
      content: '是否删除此地址？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          wx.request({
            url: Keys.data.url_Address_Del,
            data: {
              DataType: "Address_Del",
              haid: a_id,
              uid: app.login_id,
            },
            success: function (res) {
              if (res.data.isok == "true") {
                _this.data.addressList.splice(index, 1)
                _this.setData({
                  addressList: _this.data.addressList,
                })
                CommonUtils.showToastWithImg("删除成功", "")
              }
            }
          })
        }
      }
    })
  },

  addAddess: function () {
    wx.navigateTo({
      url: "/pages/address-add/index"
    })
  },

  selectAddress: function (e) {
    if (this.data.isSelectAddress) {
      var addressBean = this.data.addressList[e.currentTarget.dataset.index];
      // wx.navigateBack({})
      wx.setStorage({
        key: Keys.data.key_select_address,
        data: addressBean,
        success: function () {
          wx.navigateBack({})
        }
      })
    }
  },


  // initShippingAddress: function () {
  //   var that = this;
  //   wx.request({
  //     url: 'https://api.it120.cc/' + app.globalData.subDomain + '/user/shipping-address/list',
  //     data: {
  //       token: app.globalData.token
  //     },
  //     success: (res) => {
  //       if (res.data.code == 0) {
  //         that.setData({
  //           addressList: res.data.data
  //         });
  //       } else if (res.data.code == 700) {
  //         that.setData({
  //           addressList: null
  //         });
  //       }
  //     }
  //   })
  // }

})
